package com.main.common.configs;

import cn.dev33.satoken.stp.StpUtil;
import com.main.common.interceptors.LocaleInterceptor;
import com.main.common.interceptors.SessionLocaleInterceptor;
import com.main.common.utils.ConfigUtil;
import com.main.common.utils.SaTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.time.ZoneId;
import java.util.Locale;
import java.util.TimeZone;

@Slf4j
@Configuration
public class LocaleResolverConfig  implements WebMvcConfigurer {

    private final String lang =  ConfigUtil.getProperty("Default.Locale");
    private final String timeZone =  ConfigUtil.getProperty("Default.TimeZone");

    /**
     * 本地化设置
     */
    @Primary
    @Bean(name="localeResolver")
    public LocaleResolver localeResolver() {
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        log.info(lang);
        log.info(timeZone);
        try {
            Locale locale = Locale.of(lang.split("_")[0], lang.split("_")[1]);
            localeResolver.setDefaultLocale(locale);
            localeResolver.setDefaultTimeZone(TimeZone.getTimeZone(ZoneId.of(timeZone)));
        }catch (Exception e) {
            localeResolver.setDefaultLocale(Locale.CHINA);
            localeResolver.setDefaultTimeZone(TimeZone.getTimeZone(ZoneId.of(timeZone)));
        }
        return localeResolver;
    }

    @Bean(name="sessionLocaleResolver")
    public SessionLocaleResolver sessionLocaleResolver() {
        SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
        try {
            Locale locale = Locale.of(lang.split("_")[0], lang.split("_")[1]);
            sessionLocaleResolver.setDefaultLocale(locale);
            sessionLocaleResolver.setDefaultTimeZone(TimeZone.getTimeZone(ZoneId.of(timeZone)));
        }catch (Exception e) {
            sessionLocaleResolver.setDefaultLocale(Locale.CHINA);
            sessionLocaleResolver.setDefaultTimeZone(TimeZone.getTimeZone(ZoneId.of("Asia/Shanghai")));
        }
        return sessionLocaleResolver;
    }

//    @Override
//    public void addInterceptors(InterceptorRegistry registry){
//        System.out.println("LocaleInterceptor");
//        LocaleInterceptor localeInterceptor =new LocaleInterceptor();
//        registry.addInterceptor(localeInterceptor).addPathPatterns("/**");//拦截所有路径,/**表示匹配多层地址
//    }

    @Override
    public void addInterceptors(InterceptorRegistry registry){
        System.out.println("LocaleInterceptor");
        SessionLocaleInterceptor sessionLocaleInterceptor =new SessionLocaleInterceptor();
        registry.addInterceptor(sessionLocaleInterceptor).addPathPatterns("/**");//拦截所有路径,/**表示匹配多层地址
    }
}
